package com.root.dreameraadmin.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;

import com.root.dreameraadmin.R;

/**
 * Created by acer on 2016/9/21.
 */

@SuppressLint("NewApi")
public class ReFlashRecyclerView extends RecyclerView implements View.OnScrollChangeListener {

    View header;//headerview
    int headerheight;
    int firstVisibleItem;//first visible item location

    int scrollState;

    boolean isRemark;
    int startY;

    int state;//state
    final int NONE=0;//normal
    final int PULL = 1;// 提示下拉状态；
    final int RELESE = 2;// 提示释放状态；
    final int REFLASHING = 3;// 刷新状态；

    ReFlashlistView.IReflashListener iReflashListener;//update data interface



    public ReFlashRecyclerView(Context context) {
        super(context);
//        initView(context);
    }

    public ReFlashRecyclerView(Context context, AttributeSet attrs) {
        super(context, attrs);
//        initView(context);
    }
    public ReFlashRecyclerView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
//        initView(context);
    }

    private void initView(Context context){
        LayoutInflater inflater=LayoutInflater.from(context);
        header =inflater.inflate(R.layout.header_layout,null);
//        measureView(header);
        headerheight=header.getMeasuredHeight();
        Log.i("tag","headerHeight="+headerheight);
//        topPadding(-headerheight);
//        this.addHeaderView(header);
//        this.setOnScrollListener((AbsListView.OnScrollListener) this);
    }

    /**
     * 通知父布局占用的宽和高
     * @param view
     */
    private void  measureView(View view){
        ViewGroup.LayoutParams p=view.getLayoutParams();
        if(p==null){
            p=new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);
        }
        int width=ViewGroup.getChildMeasureSpec(0,0,p.width);
        int height;
        int tempheight=p.height;
        if(tempheight>0){
            height=MeasureSpec.makeMeasureSpec(tempheight,MeasureSpec.EXACTLY);
        }else{
            height=MeasureSpec.makeMeasureSpec(0,MeasureSpec.UNSPECIFIED);
        }
        view.measure(width,height);
    }
    /**
     * 设置header布局的上边距
     * @param topPadding
     */
    private void topPadding(int topPadding){
        header.setPadding(header.getPaddingLeft(),topPadding,header.
                getPaddingRight(),header.getPaddingBottom());
        header.invalidate();

    }

    public void onScroll(RecyclerView view, int firstVisibleItem,
                         int visibleItemCount, int totalItemCount) {

        this.firstVisibleItem = firstVisibleItem;
    }




    @Override
    public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {

    }
}
